 <?php

    #Ensure that the client has provided a value for "FirstAddressToSearch"
        
        #Setup variables
       

        
        #Connect to Database
        $con = mysqli_connect("localhost","root","", "test");
        
        #Check connection
        if (mysqli_connect_errno()) {
            echo 'Database connection error: ' . mysqli_connect_error();
            exit();
        }
	$userdetails=0;
	$firstlat = 0;
	$firstlon = 0;
	$firstlatrange1 = 0;
	$firstlatrange2 = 0;
	$firstlonrange1 = 0;
	$firstlonrange2 = 0;
        

        $radius = (float) 0.25;
 	$firstlat = (float) $_POST["FirstLatToSearch"];
	$firstlatrange1 = (float) $firstlat - ($radius / 69);
	$firstlatrange2 = (float) $firstlat + ($radius / 69);
	$firstlon = (float) $_POST["FirstLonToSearch"];
	$firstlonrange1 = (float) abs($firstlon + $radius / abs(cos(deg2rad($firstlat)) * 69));
	$firstlonrange2 = (float) abs($firstlon - $radius / abs(cos(deg2rad($firstlat)) * 69));
        $query = "SELECT * FROM coord WHERE (abs(y) BETWEEN $firstlatrange1 and $firstlatrange2 AND abs(x) BETWEEN $firstlonrange1 AND $firstlonrange2) LIMIT 50";
        $userdetails = mysqli_query($con, $query);

        #If no data was returned, check for any SQL errors
        if (!$userdetails) {
            echo 'Could not run query: ' . mysqli_error($con);
            exit;
}
        
	while($e=mysqli_fetch_assoc($userdetails))
	{
		$output[]=$e;
	}
        #Output the JSON data
        echo json_encode($output); 
?>